package com.example.interactservice.interception;

import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

@Controller
public class WebSocketMessageHandler implements WebSocketHandler {

    @MessageMapping("/pushMessage") // 客户端请求接收路径
    @SendTo("/topic/messages") // 推送给客户端的路径
    public String pushMessageToUser(String message) {
        // 在这里处理消息，并推送给连接的用户
        // 实际应用中，可以使用推送服务、实时通信工具等来将消息推送给用户
        // 这里仅作示例，向客户端广播收到的消息
        return message;
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
        System.out.println("用户上线");
    }

    @Override
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {

    }

    @Override
    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {

    }

    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {

    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}